package com.vcredit.bird.service.netCredit;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.api.INetCreditService;
import com.vcredit.bird.service.base.BirdResponse;
import com.vcredit.bird.service.base.BirdService;
import com.vcredit.bird.util.Jmail;
import com.vcredit.bird.vo.netcredit.BankcardInitParam;
import com.vcredit.bird.vo.netcredit.BankcardSubmitParam;
import com.vcredit.bird.vo.netcredit.EmailParam;
import com.vcredit.bird.vo.netcredit.LoginParam;
import com.vcredit.bird.vo.netcredit.QuestionFirstStepParam;
import com.vcredit.bird.vo.netcredit.QuestionSecondParam;
import com.vcredit.bird.vo.netcredit.RegisterFirstStepParam;
import com.vcredit.bird.vo.netcredit.RegisterSecondStepParam;
import com.vcredit.bird.vo.netcredit.RegisterThirdStepParam;
import com.vcredit.bird.vo.netcredit.ReportAllParam;
import com.vcredit.bird.vo.netcredit.ReportParam;
import com.vcredit.bird.vo.netcredit.ResultParam;
import com.vcredit.bird.vo.netcredit.SummaryParam;
import com.vcredit.bird.vo.netcredit.UnionpaySubmitParam;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.JacksonUtil;

@Service
public class NetCreditService extends BirdService implements INetCreditService {
	@Value("${credit.bank.init}")
	private String INIT_URL;
	@Value("${credit.bank.register1}")
	private String REGISTER1_URL;
	@Value("${credit.bank.register2}")
	private String REGISTER2_URL;
	@Value("${credit.bank.register3}")
	private String REGISTER3_URL;
	@Value("${credit.bank.login}")
	private String LOGIN_URL;
	@Value("${credit.bank.apply1}")
	private String APPLY1_URL;
	@Value("${credit.bank.apply2}")
	private String APPLY2_URL;
	@Value("${credit.bank.result}")
	private String RESULT_URL;
	@Value("${credit.bank.report}")
	private String REPORT_URL;
	@Value("${credit.bank.summary}")
	private String SUMMARY_URL;
	@Value("${credit.bank.card.init}")
	private String CARD_INIT_URL;
	@Value("${credit.bank.card.submit}")
	private String CARD_SUBMIT_URL;
	@Value("${credit.bank.unionpay.init}")
	private String UNIONPAY_INIT;
	@Value("${credit.bank.unionpay.check}")
	private String UNIONPAY_CHECK;
	@Value("${credit.bank.unionpay.sms}")
	private String UNIONPAY_SMS;
	@Value("${credit.bank.unionpay.submit}")
	private String UNIONPAY_SUBMIT;
	@Value("${credit.bank.recorddtl}")
	private String RECORDDTL_URL;
	@Value("${credit.bank.report1}")
	private String REPORT1_URL;
	@Value("${credit.bank.reportAll}")
	private String REPORT_ALL;
	@Value("${credit.bank.getbasic}")
	private String GETBASIC_URL;
	@Value("${credit.bank.getcardage}")
	private String GETCARDAGE_URL;
	@Value("${credit.bank.calculate}")
	private String CALCULATE_URL;
	@Value("${credit.bank.white.card}")
	private String XIAOBAI_URL;

	@Value("${report.url}")
	private String reportBaseUrl;
	@Value("${report.file.path}")
	private String reportPath;
	@Value("${sendreport.mail.host}")
	private String emailhost;
	@Value("${sendreport.mail.account}")
	private String sendAccount;
	@Value("${sendreport.mail.password}")
	private String sendPassword;
	@Value("${sendernick}")
	private String sendernick;

	@Log
	@Override
	public BirdResponse netInit() throws Exception {
		return  super.getBirdData(INIT_URL);
	}

	@Log
	@Override
	public BirdResponse netRegister1(RegisterFirstStepParam param)
			throws Exception {
		return this.getBirdResponse(REGISTER1_URL, JacksonUtil.Obj2Json(param));
	}
	@Log
	@Override
	public BirdResponse netRegister2(RegisterSecondStepParam param)
			throws Exception {
		return this.getBirdResponse(REGISTER2_URL, JacksonUtil.Obj2Json(param));
	}
	@Log
	@Override
	public BirdResponse netRegister3(RegisterThirdStepParam param)
			throws Exception {
		return this.getBirdResponse(REGISTER3_URL, JacksonUtil.Obj2Json(param));
	}
	@Log
	@Override
	public BirdResponse netLogin(LoginParam param) throws Exception {
		return this.getBirdResponse(LOGIN_URL, JacksonUtil.Obj2Json(param));
	}
	@Log
	@Override
	public BirdResponse netApply1(QuestionFirstStepParam param)
			throws Exception {
		return this.getBirdResponse(APPLY1_URL, JacksonUtil.Obj2Json(param));
	}
	@Log
	@Override
	public BirdResponse netApply2(QuestionSecondParam param) throws Exception {
		return this.getBirdResponse(APPLY2_URL, JacksonUtil.Obj2Json(param));
	}
	@Log
	@Override
	public BirdResponse netResult(ResultParam param) throws Exception {
		return this.getBirdResponse(RESULT_URL, JacksonUtil.Obj2Json(param));
	}
	@Override
	@Log
	public BirdResponse netReport(ReportParam param) throws Exception {
		return this.getBirdResponse(REPORT_URL, JacksonUtil.Obj2Json(param));
	}
	@Log
	@Override
	public BirdResponse summary(SummaryParam param) throws Exception {
		return this.getBirdResponse(SUMMARY_URL, JacksonUtil.Obj2Json(param));
	}
	
	@Log
	@Override
	public BirdResponse recorddtl(String reportId) throws Exception {
		String url = RECORDDTL_URL.replace("{reportid}", reportId);
		return  super.getBirdData(url);
	}
	
	@Log
	@Override
	public BirdResponse netBankcardInit(BankcardInitParam param) throws Exception {
		return this.getBirdResponse(CARD_INIT_URL, JacksonUtil.Obj2Json(param));
	}

	@Log
	@Override
	public BirdResponse netBankcardSubmit(BankcardSubmitParam param)
			throws Exception {
		return this.getBirdResponse(CARD_SUBMIT_URL, JacksonUtil.Obj2Json(param));
	}

	@Log
	@Override
	public BirdResponse netUnionpaySubmit(UnionpaySubmitParam param)
			throws Exception {
		return this.getBirdResponse(UNIONPAY_SUBMIT, JacksonUtil.Obj2Json(param));
	}

	@Override
	public BirdResponse reportsn(String reportSn) throws Exception {
		return null;
	}

	@Log
	@Override
	public BirdResponse reportAll(ReportAllParam param) throws Exception {
		return this.getBirdResponse(REPORT_ALL, JacksonUtil.Obj2Json(param));
	}

	@Log
	@Override
	public void netSendReport(EmailParam param) throws Exception {
		Date date = new Date();
		String dateStr = new SimpleDateFormat("yyyyMMdd").format(date);
		String dateStr1 = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
		String file = reportPath + dateStr + File.separator + "report-"
				+ param.getReportId() + "-" + dateStr1 + ".html";
		String reportUrl = reportBaseUrl + param.getReportId();
		String html = Jmail.getHtml(reportUrl);
		FileUtils.writeByteArrayToFile(new File(file), html.getBytes("gbk"));
		try{
			Jmail.sendReport(sendernick, param.getUserName(), "征信报告", "",
					param.getEmail(), sendAccount, sendPassword, emailhost, html
							+ "<br><hr>本邮件为系统自动发送，请勿回复！！！<br><br>", file,
					"征信报告.html");
		}catch(Exception e){
			throw new Exception("邮件发送失败！");
		} finally{
			FileUtils.deleteQuietly(new File(file));
		}
	}
	
	private BirdResponse getBirdResponse(String url, String jsonParam) throws Exception {
		BirdResponse tmpResponse = super.postBirdData(url, jsonParam);
		if(tmpResponse.getResult()==null){
			return tmpResponse;
		}
		BirdResponse response;
		//可能返回的不是json而是form表单之类，此时不进行转换
		try {
			response = JacksonUtil.Json2Obj(tmpResponse.getResult(), new TypeReference<BirdResponse>() {
			});
		} catch (Exception e) {
			return tmpResponse;
		}
		return com.vcredit.creditcat.util.BeanUtil.copy(tmpResponse, response, true);
	}

}
